import { Input, linkDir, log } from '../../core';
import { ActionBase } from '../action-base/action-base';
import { resolve, basename } from 'path';
import * as fs from 'fs';

/**
 * 对「文件、文件夹」建立软链接
 *
 * @author chitanda
 * @date 2021-04-18 13:04:24
 * @export
 * @class LinkModelAction
 * @extends {ActionBase}
 */
export class LinkModelAction extends ActionBase {
  async handle(_input: Input, options: Input, _extraFlags?: string[]): Promise<void> {
    const { name, force, modelPath } = options;
    const appPath = resolve('');
    const pkgJson = resolve(appPath, 'package.json');
    // 根据package.json判断是否为应用目录
    if (!fs.existsSync(pkgJson)) {
      log.error('', `请检查当前执行是否为应用文件夹!`);
      return;
    }
    const appName = name.value || basename(appPath).substring(4);
    const sourcePath = resolve('../', modelPath.value, 'PSSYSAPPS', appName);
    if (!fs.existsSync(sourcePath)) {
      log.error('', `需要链接的模型目录[${sourcePath}]不存在，请检查!`);
      return;
    }
    const targetPath = resolve('./public/assets/model');
    linkDir(sourcePath, targetPath, force.value);
  }
}
